package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.User;
import org.example.rowing.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User showUserByName(String userName) {
        return userMapper.selectUserByName(userName);
    }

    public List<User> showAllUsers() {
        return userMapper.selectAllUsers();
    }

    public void saveOrUpdateUser(User user) {
        if (user == null || user.getUserName() == null ||
                user.getPasswordHash() == null || user.getRequirePasswordChange() == null) {
            throw new RuntimeException("用户信息补全，无法保存");
        }
        User user1 = userMapper.selectUserByName(user.getUserName());
        if (user1 == null) {
            userMapper.insertUser(user);
        } else {
            userMapper.updateUser(user);
        }
    }
}
